package com.sanqing.iframe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import com.sanqing.dao.Dao;
import com.sanqing.iframe.BookModiAndDelIFrame.TableListener;
import com.sanqing.model.*;

public class RecordCheck extends JInternalFrame{
	
    private String[] columnNames;
    private JTable table;
    
	 private Object[][] getFileStates(List list) {
	        String[] columnNames = { "图书编号", "图书类别", "图书名称", "作者", "译者", "出版商", "语言", "验收日期", "数量","价格","管理员" }; // 新增：价格列
	        Object[][] results = new Object[list.size()][columnNames.length];
	        for (int i = 0; i < list.size(); i++) {
	            RecordInfo recordinfo = (RecordInfo) list.get(i);
	            results[i][0] = recordinfo.getISBN();
	            Integer typeId = recordinfo.getTypeid();
	            String bookTypeName = MapPz.getIdToName().get(typeId);
	            results[i][1] = bookTypeName;
	            results[i][2] = recordinfo.getBookname();
	            results[i][3] = recordinfo.getWriter();
	            results[i][4] = recordinfo.getTranslator();
	            results[i][5] = recordinfo.getPublisher();
	            results[i][6] = recordinfo.getLanguage();
	            results[i][7] = recordinfo.getDate();
	            results[i][8] = recordinfo.getNumber(); // 推荐理由
	            results[i][9] = recordinfo.getPrices(); // 推荐理由
	            results[i][10] = recordinfo.getOperator(); // 推荐理由
	        }
	        return results;
	    }
	 public RecordCheck()
	 {
		 super();
	        final BorderLayout borderLayout = new BorderLayout();
	        getContentPane().setLayout(borderLayout);
	        setIconifiable(true);
	        setClosable(true);
	        setTitle("验收记录");
	        setBounds(20, 100, 850, 406); // 新增：调整窗体宽度以容纳新控件
	        final JPanel mainPanel = new JPanel();
	        final BorderLayout borderLayout_1 = new BorderLayout();
	        borderLayout_1.setVgap(5);
	        mainPanel.setLayout(borderLayout_1);
	        mainPanel.setBorder(new EmptyBorder(5, 10, 5, 10));
	        getContentPane().add(mainPanel);
	        final JScrollPane scrollPane = new JScrollPane();
	        mainPanel.add(scrollPane);

	        Object[][] results = getFileStates(Dao.selectRecordInfo());
	        // 新增：在列名中添加"语言"
	        columnNames = new String[] { "图书编号", "图书类别", "图书名称", "作者", "译者", "出版商", "语言", "验收日期","数量","价格","操作员" };
	        table = new JTable(results, columnNames) {
	            @Override
	            public boolean isCellEditable(int row, int column) {
	                return false; // 所有单元格都不可编辑
	            }
	        };
	        PopupMenu popupMenu = new PopupMenu(table, this, columnNames);
	        table.addMouseListener(popupMenu.getTableListener());
	        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	        scrollPane.setViewportView(table);
	        
	        // 底部按钮
	        final JPanel bottomPanel = new JPanel();
	        bottomPanel.setBorder(new LineBorder(new java.awt.Color(139, 137, 137), 1, false));
	        getContentPane().add(bottomPanel, BorderLayout.SOUTH);

	        final FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT, 30, 2);
	        bottomPanel.setLayout(flowLayout);
	        
	        final JButton closeButton = new JButton("关闭");
	        closeButton.addActionListener(e -> doDefaultCloseAction());
	        bottomPanel.add(closeButton);
	        
	        // 头部图标
	        final JLabel headLogo = new JLabel();
	        ImageIcon bookModiAndDelIcon = new ImageIcon("res/login.jpg");
	        headLogo.setIcon(bookModiAndDelIcon);
	        headLogo.setOpaque(true);
	        headLogo.setBackground(new java.awt.Color(173, 216, 230));
	        headLogo.setPreferredSize(new Dimension(400, 80));
	        headLogo.setBorder(new LineBorder(new java.awt.Color(139, 137, 137), 1, false));
	        getContentPane().add(headLogo, BorderLayout.NORTH);
	        
	     // 新增：添加窗体焦点监听
	        addFocusListener(new FocusAdapter() {
	            @Override
	            public void focusGained(FocusEvent e) {
	                refreshTable(); // 窗体获得焦点时刷新表格
	            }
	        });
	        setVisible(true);
	 }
	 // 刷新表格
	    private void refreshTable() {
	        Object[][] results = getFileStates(Dao.selectRecordInfo());
	        DefaultTableModel model = new DefaultTableModel(results, columnNames);
	        table.setModel(model);
	    }
}
